package miku.firstgame.world
{
	import com.saia.starlingPunk.SP;
	import com.saia.starlingPunk.SPWorld;
	
	
	import feathers.controls.Button;
	import feathers.controls.Label;
	import feathers.controls.TextInput;
	import feathers.core.DisplayListWatcher;
	import feathers.layout.HorizontalLayout;
	import feathers.themes.MetalWorksMobileTheme;
	
	import miku.firstgame.event.LoginEvent;
	import miku.firstgame.util.StringUtil;
	import miku.firstgame.view.LoginWorldMediator;
	
	import org.puremvc.as3.patterns.facade.Facade;
	
	import starling.events.Event;

	public class LoginWorld extends SPWorld
	{

		private var _theme:DisplayListWatcher;
		private var label:Label;
		public var button:Button;
		private var input:TextInput;

		public function LoginWorld()
		{
			super();
		}

		override public function begin():void
		{
			super.begin(); 
			initUI();
			Facade.getInstance().registerMediator(new LoginWorldMediator(this));
		}

		override public function end():void
		{
			super.end();
			removeAll();
			removeChildren(0, -1, true);
			Facade.getInstance().removeMediator(LoginWorldMediator.NAME);
		}

		private function initUI():void
		{
			//TODO 移动到初始化位置
			this._theme = new MetalWorksMobileTheme(this.stage); //new MinimalMobileTheme(this.stage); //new MetalWorksMobileTheme(this.stage); // new MinimalMobileTheme(this.stage); //MetalWorksMobileTheme(this.stage);
 
			label = new Label();
			label.text = "请输入您的用户名:";
//			label.width = 250;
//			label.
			//label.
			label.validate();
			label.y = 30;
			addChild(label);

			button = new Button();
			button.addEventListener(Event.TRIGGERED, onButtonClick);
			button.label = "确定";
			//button.isToggle = true;
			button.width = 60;
			button.height = 40;
			addChild(button);

			input = new TextInput();
			input.width = 150;
			input.height = 40;
			addChild(input);
			resize();
		}

		public function resize():void
		{

			input.x = SP.width - input.width - button.width - 10 >> 1;
			button.x = input.x + input.width + 10;
			button.y = input.y = 200;
			label.x = input.x;
			label.y = input.y - 35;
		}

		//override public function 

		private function onButtonClick(e:Event):void
		{
			var event:LoginEvent = new LoginEvent(LoginEvent.USER_LOGIN, StringUtil.trim(input.text));
			dispatchEvent(event);
		}
	}
}
